﻿using System;
using System.IO;
using System.Net;
using System.Linq;
using System.Data;
using System.Text;
using System.Activities;
using System.Activities.Expressions;
using System.Activities.Presentation.PropertyEditing;
using System.Activities.Statements;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml;

namespace Vision.Core.ActivityComponents.SharePoint
{
    /// <summary>
    /// 
    /// </summary>
    [System.ComponentModel.Designer(typeof(SPListGetListItemsDesigner))]
    public class SPListGetListItems : NativeActivity
    {
        /// <summary>
        /// 
        /// </summary>
        [Browsable(false)]
        [DefaultValue(null)]
        public ActivityAction<DataRow> Body { get; set; }
        
        /// <summary>
        /// 
        /// </summary>
        [DefaultValue("")]
        [Category("Input")]
        public InArgument<string> ListName { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [DefaultValue("")]
        [Category("Input")]
        public InArgument<string> ViewName { get; set; }

        /// <summary>
        /// 
        /// </summary>
        [DefaultValue("")]
        [Category("Input")]
        public InArgument<string> Query { get; set; }
                
        /// <summary>
        /// 
        /// </summary>
        public SPListGetListItems() : base()
        {
            this.Body = new ActivityAction<DataRow>(){
                Argument = new DelegateInArgument<DataRow>() { Name = "dataRow" }
            };                        
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(NativeActivityContext context)
        {
            XmlDocument xDoc = new XmlDocument();
            xDoc.LoadXml(this.Query.Get(context));
            
            List<object> methodParams = new List<object>();
            methodParams.Add(this.ListName.Get(context));
            methodParams.Add(this.ViewName.Get(context));
            methodParams.Add(xDoc.DocumentElement);
            methodParams.Add(null);
            methodParams.Add(null);
            methodParams.Add(null);
            methodParams.Add(null);
            
            Components.DynamicWebService spService = (Components.DynamicWebService)context.Properties.Find("F1634880-5000-11E0-A748-B487DFD72085");
            if (spService != null){
                DataSet dataSet = new DataSet();
                XmlElement result = spService.InvokeMethod<XmlElement>("GetListItems", methodParams);
                using (XmlTextReader reader = new XmlTextReader(result.OuterXml, XmlNodeType.Element, null)){
                    dataSet.ReadXml(reader);
                }

                DataTable dTable = dataSet.Tables["row"];
                if (dTable != null){
                    foreach (DataRow row in dTable.Rows){
                        context.ScheduleAction(this.Body, row);
                    }
                }
            }
        }
    }
}
